/* 首页路由模块 */
const express = require('express');
const home = express.Router();

// -------------------------------------------------
home.get('/goods', require('./home/goods/list'))
home.post('/goods', require('./home/goods/add'))
home.put('/goods', require('./home/goods/edit'))
home.delete('/goods', require('./home/goods/delete'))
home.get('/goods/:id', require('./home/goods/info'))
// 获取分类
home.get('/category', require('./home/category/list'))
// 添加商品
home.post('/goods', require('./home/goods/add'))
home.get('/goods', require('./home/goods/list'))
home.get('/goods/:id', require('./home/goods/info'))
home.delete('/goods', require('./home/goods/delete'))
home.put('/goods', require('./home/goods/edit'))
home.get('/goods/:id', require('./home/goods/info'))
// 登录
home.post('/login', require('./home/user/login'))
home.post('/login/wx', require('./home/user/wx-login'))

// 注册
home.post('/register', require('./home/user/register'))

// 用户接口
// 获取首页内容
const { childCategory } = require('../model/childCategory')
home.get('/home', require('./home/page/home'))
home.get('/home', require('./home/page/home'))
home.get('/fan/goods', require('./home/fan/goods'))

// 动态
home.post('/news', require('./home/news/add'))
home.get('/news', require('./home/news/list'))
home.get('/news/:id', require('./home/news/info'))
home.delete('/news', require('./home/news/delete'))


home.put('/avatar', require('./home/user/edit'))
home.post('/cart', require('./home/cart/add'))
home.get('/cart', require('./home/cart/list'))
home.delete('/cart', require('./home/cart/delete'))

// 粉丝调用
home.get('/fan/news', require('./home/fan/news'))

// 获取商家主页
home.get('/fan/business-home', require('./home/page/business-home'))
home.get('/date', (req, res) => {
  res.json({
    data: {
      timestamp: new Date().getTime()
    }
  })
})

home.get('/chat/:toUser', require('./home/message/info'))
home.get('/chat', require('./home/message/list'))
home.put('/chat', require('./home/message/edit'))

home.get('/friend', require('./home/friend/list'))
home.post('/friend', require('./home/friend/add'))


// 上传图片
const multer = require('multer')
const path = require("path")

var storage = multer.diskStorage({
    // 配置文件上传后存储的路径
    destination: function(req, file, cb) {
        cb(null, path.join(__dirname, '../public/upload'))
    },
    // 配置文件上传后存储的路径和文件名
    filename: function(req, file, cb) {
        console.log('file', file);
        // cb(null, Date.now() + path.extname(file.originalname))
        cb(null, Date.now() + file.originalname)
    }
})
var upload = multer({
    storage: storage
})
home.post('/upload', upload.single('uploadImage'), function(req, res, next) {
    console.log(req.file)
    const paths = req.file.path.split('\\')
    const len = paths.length
    res.json({
        "errno": 0, // 注意：值是数字，不能是字符串
        "data": {
            "url": 'http://localhost:8888' + '/' + paths[len - 2] + '/' + paths[len - 1], // 图片 src ，必须
        }
    })
})

module.exports = home;